package com.Business.web;

import com.Base.BaseServlet;
import com.Base.ResultCode;
import com.Login.Service.LoginService;
import com.Pojo.User;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by XingYang on 2019/3/19.
 */
@Controller
@RequestMapping(value = "authorize.spr")
public class AuthorizeController extends BaseServlet {

    @Autowired
    LoginService loginService;


/**
 * 获取该用户所属主账号创建的角色 以及sys创建的角色
 */

    @RequestMapping(params = "method=getAllRoles")
    @ResponseBody
    public String getAllRoles(@RequestParam int pageNum,@RequestParam int pageSize, HttpServletRequest request){
        HttpSession session =request.getSession();
        User user  = (User)session.getAttribute("user");

        if (StringUtils.isEmpty(user.getMainaccount())){
           return sendPageResponse(ResultCode.SUCCESS,"success",loginService.getRolesInfByUid(user.getId(),pageNum,pageSize)) ;
        }else{
            String mainaccount = loginService.getUserById(user.getMainaccount()).getId();
            PageHelper.startPage(pageNum,pageSize);
            return sendPageResponse(ResultCode.SUCCESS,"success",loginService.getRolesInfByUid(mainaccount,pageNum,pageSize)) ;
        }

    }
    @RequestMapping(params = "method=getAllPowers")
    @ResponseBody
    public String getAllPowers(){
        Map map = new HashMap();
        map.put("powers",loginService.getAllPowers());
        return sendResponse(ResultCode.SUCCESS,"msg",map);
    }
    @RequestMapping(params = "method=createRole",method = RequestMethod.POST)
    @ResponseBody
    public String createRole(@RequestBody Map<String,String> map,HttpServletRequest request){
        if(!map.containsKey("roleName") || !map.containsKey("roleCode") || !map.containsKey("powers")){
            return sendResponse(ResultCode.NullArguments,"错误的参数",null);
        }
        HttpSession session = request.getSession();
        User user  = (User)session.getAttribute("user");
        map.put("userid",user.getId());
        loginService.addRole(map);



        return sendResponse(ResultCode.SUCCESS,"success",null);
    }
    @RequestMapping(params = "method=getRolePowers",method = RequestMethod.POST)
    @ResponseBody
    public String getRolePowers(@RequestBody Map map){
        String roleid = (String) map.get("roleId");
        map.put("powers",loginService.getPowersByRid(roleid));
         return  sendResponse(ResultCode.SUCCESS,"success",map);

    }
    @RequestMapping(params = "method=midifyRole",method = RequestMethod.POST)
    @ResponseBody
    public String modifyRole(@RequestBody Map<String,String> map,HttpServletRequest request){
        if(!map.containsKey("roleId")||!map.containsKey("roleName") || !map.containsKey("roleCode") || !map.containsKey("powers")){
            return sendResponse(ResultCode.NullArguments,"错误的参数",null);
        }
        HttpSession session = request.getSession();
        User user  = (User)session.getAttribute("user");
        map.put("userid",user.getId());
        if (!loginService.modifyRole(map)){
            return sendResponse(ResultCode.Failed,"failed",null);
        }

        return sendResponse(ResultCode.SUCCESS,"success",null);
    }
    @RequestMapping(params = "method=getRoleInf",method = RequestMethod.POST)
    @ResponseBody
    public String getRoleInfByRid(@RequestBody Map map){
        if(!map.containsKey("roleId")){
            return sendResponse(ResultCode.NullArguments,"参数缺失",null);
        }
        String rid = (String) map.get("roleId");
        map = loginService.getRoleInfByRid(rid);
        return sendResponse(ResultCode.SUCCESS,"查询成功",map);
    }
    @RequestMapping(params = "method=delRole")
    @ResponseBody
    public String delRole(@RequestParam String roleId){

       int result =  loginService.delRole(roleId);
        if(result>0){
           return   sendResponse(ResultCode.SUCCESS,"",null);
        }
        return sendResponse(ResultCode.Failed,"删除失败",null);
    }

    @RequestMapping(params = "method=disableRole")
    @ResponseBody
    public String disableRole(@RequestParam String roleId){

        int result =  loginService.disableRoleByRId(roleId);
        if(result>0){
            return   sendResponse(ResultCode.SUCCESS,"",null);
        }
        return sendResponse(ResultCode.Failed,"停用失败",null);
    }
    @RequestMapping(params = "method=enableRole")
    @ResponseBody
    public String enableRole(@RequestParam String roleId){

        int result =  loginService.enableRoleByRId(roleId);
        if(result>0){
            return   sendResponse(ResultCode.SUCCESS,"",null);
        }
        return sendResponse(ResultCode.Failed,"启用失败",null);
    }















}
